<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class angsuran extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();

        $this->load->library('auth');
        $this->load->library('base');
        $this->load->library('ajaxresponse');
        $this->load->library('datatables');
        $this->load->model("report/m_potongan");
        $this->load->model("crud/m_info_usaha");
    }

    function index(){
        $this->edit();
    }

    function anggota()
    {
        $this->auth->is_logged_in('form/login');
        $this->response = new ajaxresponse();
        $id_anggota = $this->input->post("ID_ANGGOTA", true);

        $this->load->model("crud/m_pinjaman");


        $this->response->data["pinjaman"] = $this->m_pinjaman->get("anggota.ID_ANGGOTA = $id_anggota AND PIUTANG > 0")->result();
        $this->response->data['info_usaha'] = $this->m_info_usaha->get()->result();
        echo json_encode($this->response);
    }

    function simpan()
    {
        $this->auth->is_logged_in('form/login');
        $this->response = new ajaxresponse();
        $id_anggota = $this->input->post("ID_ANGGOTA", true);
        $result = $this->m_potongan->get_active($id_anggota);
        foreach($result->result() as $row)
        {
            $data = array(
                "POTONGAN" => $this->input->post("potongan_".$row->ID_PINJAMAN, true)
              //  "PIUTANG" => $row->PIUTANG + ( $row->POTONGAN - $this->input->post("potongan_".$row->ID_PINJAMAN, true) - $row->MARGIN ),
            );
            $this->db->where("ID_PINJAMAN = ".$row->ID_PINJAMAN);
            $this->db->update("pinjaman", $data);
        }
        $this->response->status = "Penyimpanan data baru berhasil";
        echo json_encode($this->response);
    }

    function edit(){
        $this->auth->is_logged_in('form/login');
        $this->base->head_html('Angsuran Anggota');
        $this->load->view('pengurus/br-03/edit_angsuran');
    }
}